﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Data.SqlClient;


public partial class Main_Reports_ServiceInvoice_TDSReminder : System.Web.UI.Page
{

    Datautility DU = new Datautility();
    DataTable dtGetFullDetail = new DataTable();
    DataTable dtCheckSetting = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                if (Request.QueryString != null)
                {
                    CheckSetting();
                    BindDDL();
                    BindCompDetails();
                    BindGrid();
                    BindCustomerDetail();
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message.ToString();
            }
        }
    }

    protected void CheckSetting()
    {
        string strsql = "select * from IVC_TDSMANAGEMENT_REPORTSETTING";
        dtCheckSetting = DU.getdata(strsql);
        if (dtCheckSetting.Rows.Count > 0)
        {
            if (Convert.ToBoolean(dtCheckSetting.Rows[0]["INFORMATIONYN"].ToString()) == true)
            {
                if (dtCheckSetting.Rows[0]["INFORMATION"].ToString() != string.Empty)
                {
                    lblInfo.Text = dtCheckSetting.Rows[0]["INFORMATION"].ToString();
                }
            }
            if (Convert.ToBoolean(dtCheckSetting.Rows[0]["SUBJECTYN"].ToString()) == true)
            {
                if (dtCheckSetting.Rows[0]["SUBJECT"].ToString() != string.Empty)
                {
                    lblSubject.Text = dtCheckSetting.Rows[0]["SUBJECT"].ToString();
                }
            }
            if (Convert.ToBoolean(dtCheckSetting.Rows[0]["NOTEYN"].ToString()) == true)
            {
                if (dtCheckSetting.Rows[0]["NOTE"].ToString() != string.Empty)
                {
                    lblNote.Text = dtCheckSetting.Rows[0]["NOTE"].ToString();
                }
            }  
        }
    }

    protected void BindCompDetails()
    {
        DataTable dtCompDtl = DU.getdatausingprocedure("usp_get_company_details");
        string strCompDtl = "";
       
        if (dtCompDtl.Rows.Count > 0)
        {
            if (dtCompDtl.Rows[0]["compname"].ToString() != string.Empty)
            {
                strCompDtl +="<b>"+ dtCompDtl.Rows[0]["compname"].ToString() +"</b><br />";
                lblCompYorTruly.Text = dtCompDtl.Rows[0]["compname"].ToString();
            }
            if (Convert.ToBoolean(dtCheckSetting.Rows[0]["FIRMNAMEYN"].ToString()) == true)
            {
                if (dtCheckSetting.Rows[0]["FIRMNAME"].ToString() != string.Empty)
                {
                    strCompDtl +="("+ dtCheckSetting.Rows[0]["FIRMNAME"].ToString() + ")<br />";
                }
            }
            if (dtCompDtl.Rows[0]["add1"].ToString() != string.Empty)
            {
                strCompDtl += dtCompDtl.Rows[0]["add1"].ToString();
            }
            if (dtCompDtl.Rows[0]["add2"].ToString() != string.Empty)
            {
                strCompDtl += " " + dtCompDtl.Rows[0]["add2"].ToString() + ",<br />";
            }
            if (dtCompDtl.Rows[0]["add3"].ToString() != string.Empty)
            {
                strCompDtl += dtCompDtl.Rows[0]["add3"].ToString();
            }
            if (dtCompDtl.Rows[0]["add4"].ToString() != string.Empty)
            {
                strCompDtl += " " + dtCompDtl.Rows[0]["add4"].ToString() + ",<br />";
            }
            if (dtCompDtl.Rows[0]["place"].ToString() != string.Empty)
            {
                strCompDtl += dtCompDtl.Rows[0]["place"].ToString();
            }
            if (dtCompDtl.Rows[0]["pincode"].ToString() != string.Empty)
            {
                strCompDtl += "-" + dtCompDtl.Rows[0]["pincode"].ToString() + "<br />";
            }
            if (dtCompDtl.Rows[0]["state"].ToString() != string.Empty)
            {
                strCompDtl += dtCompDtl.Rows[0]["state"].ToString();
            }
            if (dtCompDtl.Rows[0]["country"].ToString() != string.Empty)
            {
                strCompDtl += " " + dtCompDtl.Rows[0]["country"].ToString() + ".<br />";
            }
            if (dtCompDtl.Rows[0]["Phone"].ToString() != string.Empty)
            {
                strCompDtl += "Phone-" + dtCompDtl.Rows[0]["Phone"].ToString() + "<br />";
            }
            if (dtCompDtl.Rows[0]["Email"].ToString() != string.Empty)
            {
                strCompDtl += "Email-" + dtCompDtl.Rows[0]["Email"].ToString() + "<br />";
            }
            if (dtCompDtl.Rows[0]["LogoUrl"].ToString() != string.Empty)
            {
                imgLogo.ImageUrl = dtCompDtl.Rows[0]["LogoUrl"].ToString();
            }
            if (Convert.ToBoolean(dtCheckSetting.Rows[0]["PRINTLOGOYN"].ToString()) == true)
            {
                imgLogo.Visible = true;
            }
            else
            {
                imgLogo.Visible = false;
            }
        }
        lblCompany.Text = strCompDtl;
    }
    protected void BindCustomerDetail()
    {
        lblLetterNo.Text = "TDS/" + Session["finyear"].ToString() +"/"+(Convert.ToInt32(ddlCustomerName.SelectedIndex.ToString())+1).ToString();
       // lblDate.Text = string.Format("{0:dd/MMM/yyyy}", Convert.ToDateTime(DateTime.Now.ToShortDateString()));
        string lid = ddlCustomerName.SelectedValue.ToString();
        if (lid != string.Empty)
        {
            SqlParameter[] sqlparas = {
                                    new SqlParameter("@LID",lid)
                                 };

            DataTable DTMain = DU.getdatausingprocedure("USP_ACC_GetLedgerAddressDetails", sqlparas);
            string invoiceto = "";
            if (DTMain.Rows.Count > 0)
            {
                if (DTMain.Rows[0]["LNAME"].ToString() != string.Empty)
                {
                    invoiceto +="<b>"+ DTMain.Rows[0]["LNAME"].ToString() +"</b><br />";
                }
                if (DTMain.Rows[0]["Address1"].ToString() != string.Empty)
                {
                    invoiceto += DTMain.Rows[0]["Address1"].ToString();
                }
                if (DTMain.Rows[0]["Address2"].ToString() != string.Empty)
                {
                    invoiceto += " " + DTMain.Rows[0]["Address2"].ToString() + ",<br />";
                }
                if (DTMain.Rows[0]["Address3"].ToString() != string.Empty)
                {
                    invoiceto += DTMain.Rows[0]["Address3"].ToString();
                }
                if (DTMain.Rows[0]["place"].ToString() != string.Empty)
                {
                    invoiceto += " " + DTMain.Rows[0]["place"].ToString();
                }
                if (DTMain.Rows[0]["Pincode"].ToString() != string.Empty)
                {
                    invoiceto += "-" + DTMain.Rows[0]["Pincode"].ToString() + ",<br />";
                }
                if (DTMain.Rows[0]["statename"].ToString() != string.Empty)
                {
                    invoiceto += DTMain.Rows[0]["statename"].ToString();
                }
                if (DTMain.Rows[0]["Country"].ToString() != string.Empty)
                {
                    invoiceto += " " + DTMain.Rows[0]["Country"].ToString() + "<br />";
                }
                if (DTMain.Rows[0]["EMAIL"].ToString() != string.Empty)
                {
                    invoiceto += "Email-" + DTMain.Rows[0]["EMAIL"].ToString() + "<br />";
                }
                if (DTMain.Rows[0]["Website"].ToString() != string.Empty)
                {
                    invoiceto += "Website-" + DTMain.Rows[0]["Website"].ToString() + "<br />";
                }
                if (DTMain.Rows[0]["PHONE"].ToString() != string.Empty)
                {
                    invoiceto += "Phone-" + DTMain.Rows[0]["PHONE"].ToString();
                }
                lblCustomer.Text = invoiceto;
            }
        }
    }
   
    protected void BindDDL()
    {
        if (Request.QueryString != null)
        {
            string customer = Request.QueryString["Customer"].ToString();
            string custname = Request.QueryString["CustName"].ToString();

            if (customer != string.Empty && custname !=string.Empty)
            {
                ddlCustomerName.Items.Clear();
                int count = 0;
                if (customer.Contains("-") && custname.Contains("-"))
                {
                    string[] customerID = customer.Split('-');
                    string[] customersname = custname.Split('-');
                    count = customersname.Length;
                    for (int i = 0; i < count; i++)
                    {
                        if (customersname[i].ToString() != string.Empty && customerID[i].ToString() != string.Empty)
                        {
                            ddlCustomerName.Items.Add(new ListItem(customersname[i].ToString(), customerID[i].ToString()));
                        }
                    }
                }
            }
        }
    }

    protected void BindGrid()
    {
        string lid = ddlCustomerName.SelectedValue.ToString();
        if (lid != string.Empty)
        {
            SqlParameter[] sqlparad = {
                                    new SqlParameter("@CUSTOMERLEDGERID",lid)
                                 };
            DataTable dtBillDtl = DU.getdatausingprocedure("USP_IVC_GetExistingTDS", sqlparad);
            if (dtBillDtl.Rows.Count > 0)
            {
                grdBillDetail.DataSource = dtBillDtl;
                grdBillDetail.DataBind();
            }
        }
    }
    
    protected void ddlCustomerName_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid();
        BindCustomerDetail();
    }

    protected void grdBillDetail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //e.Row.Style.Add("border-left-color", "White");
            //e.Row.Style.Add("border-right-color", "White");
            //e.Row.Style.Add("border-left-style", "none");
            //e.Row.Style.Add("border-right-style", "none");
            e.Row.Style.Add("border-top-style", "solid 1px #e1e1e1");
            e.Row.Style.Add("border-bottom-style", "solid 1px #e1e1e1");
        }
    } 
 
}
